using System.ComponentModel.DataAnnotations;
using LazyFieldDemo.Helpers;
using LazyFieldDemo.Models;
using Orchard.Data;

namespace LazyFieldDemo.ViewModels {
    public class AddressViewModel {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string Zipcode { get; set; }
        public string City { get; set; }

        [DataType("Country")]
        public int? CountryId { get; set; }

        public static AddressViewModel Map(AddressPart source) {
            return Map(source, new AddressViewModel());
        }

        public static AddressViewModel Map(AddressPart source, AddressViewModel target) {
            target.AddressLine1 = source.AddressLine1;
            target.AddressLine2 = source.AddressLine2;
            target.Zipcode = source.Zipcode;
            target.City = source.City;
            target.CountryId = source.Country != null ? source.Country.Id : default(int?);
            return target;
        }

        public static void Map(AddressViewModel source, AddressPart target, IRepository<Country> countryRepository) {
            target.AddressLine1 = source.AddressLine1.TrimSafe();
            target.AddressLine2 = source.AddressLine2.TrimSafe();
            target.Zipcode = source.Zipcode.TrimSafe();
            target.City = source.City.TrimSafe();
            target.Country = source.CountryId != null ? countryRepository.Get(source.CountryId.Value) : null;
        }
    }
}